package com.djk.daily.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZoneOffset;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author dujunkai
 * @version 1.0
 * @description:
 * @date 2021/9/7 14:56
 */
public class DateUtil {
    public static void main(String[] args) {
        /**
         * SimpleDateFormat
         */
        //Date–>Text
        SimpleDateFormat myFmt1 = new SimpleDateFormat("yyyy年MM月dd日 HH时mm分ss秒");
        Date now = new Date();
        System.out.println(myFmt1.format(now));
        //Text–>Date
        String time1 = "2018年06月19日 23时10分05秒";
        SimpleDateFormat myFmt = new SimpleDateFormat("yyyy年MM月dd日 HH时mm分ss秒");
        Date date1 = null;
        try {
            date1 = myFmt.parse(time1);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        System.out.println(date1);
        /**
         * DateTimeFormatter
         */
        //Text-->Date
        String dateStr= "2018-06-20 11:25:56";
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        LocalDateTime localDateTime = LocalDateTime.parse(dateStr, dateTimeFormatter);
        ZoneId zone = ZoneId.systemDefault();
        Instant instant = localDateTime.atZone(zone).toInstant();
        Date date = Date.from(instant);

        //Date-->Text
        Date date2 = new Date();
        Instant instant1 = date2.toInstant();
        ZoneId zone1 = ZoneId.systemDefault();
        LocalDateTime localDateTime1 = LocalDateTime.ofInstant(instant1, zone1);
        DateTimeFormatter format = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        String nowStr = localDateTime1.format(format);
        System.out.println(nowStr);

        //LocalDateTime获取毫秒
        System.out.println(localDateTime.toInstant(ZoneOffset.ofHours(8)).toEpochMilli());

        String regex=".*[a-z]+.*";
        Matcher m= Pattern.compile(regex).matcher("这是个A挤");
        if (m.find()){
            System.out.println();
        }
    }
}
